/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package auctiongui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import auctioncentral.AuctionSystem;
import auctioncentral.BiddingUser;

/**
 * The main/first screen for all users.
 *
 * @author Harris
 */
public class MainPage extends JFrame implements ActionListener{

	AuctionSystem my_system;
	
    /**
     * Creates new form MainPage
     */
    public MainPage(AuctionSystem the_system) {
    	my_system = the_system;
        initComponents();
        jMenuItem1.addActionListener(this);
        jMenuItem2.addActionListener(new EditAction()); //Only placed this action is called, suspected needs changing below.
        
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                
            	my_system.saveData();
            	
            }
        });
        
    }
    
    /**
     * Saves the current data (lists) to the database
     */
    public class SaveListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			my_system.saveData();
			
		}
    	
    }
    
    /**
     * This action is called when a user wants to edit a prexisting event.
     */
    class EditAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) { //Probably needs to pass in the event as well

			//my_system.getEventList().get(0).addItems(new Item("balls", 2, my_system.getEventList().get(0).getName(), 72));
			new NewEvent(my_system, my_system.getMy_current_user(), my_system.getEventList().get(0)).setVisible(true);
			
			//This looks like test hardcode? only calling the first event in the list?
			
		}
    	
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

    	setBackground(Color.WHITE);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    	jPanel1 = new CalendarPanel(my_system);
    	add(jPanel1);
    	setPreferredSize(new Dimension(605, 650));
        setResizable(false);
        String[] array = null;
        if (my_system.getEventList().size() == 0 || my_system.getEventList() == null) {
        	array = new String[0];
        } else {
        	array = new String[my_system.getEventList().size()];
        }
        
        for(int x = 0; x < my_system.getEventList().size(); x++)
		{
          array[x] = my_system.getEventList().get(x).getName();
		}
        

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        JButton bid_button = new JButton("My Bids");
        if (my_system.getMy_current_user() instanceof BiddingUser) {
        	bid_button.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					
					if (my_system.findBidsFromUser(my_system.getMy_current_user().getName()) == null) {
						JOptionPane.showMessageDialog(null, "You have no bids.", "Error.", JOptionPane.ERROR_MESSAGE);
					} else {
						new MyBidsPopup(my_system.findBidsFromUser(my_system.getMy_current_user().getName()), my_system).setVisible(true);	
					}
				}
        		
        	});
        	jMenuBar1.add(bid_button);
        }
        

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 375, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );


        jMenu1.setText("File");

        jMenuItem1.setText("New Event");
        jMenuItem2.setText("Edit Event");
        jMenu1.add(jMenuItem1);
        jMenu1.add(jMenuItem2);

//        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);


        pack();
    }// </editor-fold>//GEN-END:initComponents

   

    // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    
    /**
     * This action is fired when a user goes to create a new event.
     */
    @Override
    public void actionPerformed(ActionEvent e) {        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                
            }
        });
    }
}
